import './common/base.scss'
import 'bootstrap/dist/css/bootstrap.min.css'
import './sass/detail.scss'

import {strToObj} from './common/util'
import axios from 'axios';

// 导入header的结构
import {createHtml} from './common/headerHtml'

let header1=document.querySelector('.header1 .jd-container')
header1.innerHTML=createHtml('./')

// 导入header的实现功能
import {header} from './common/header'
header()

/* 
    1.获取地址栏中goods_id
    2.根据goods_id 查询这个商品的数据
    3.根据查询出来的数据渲染结构
    4.点击添加购物车把商品加入到购物车中
        发送加入购物车的请求
*/

let title = document.querySelector('.title')
let goodsImg = document.querySelector('.goodsImg');
let price = document.querySelector('.price');
let introduce_conetnt = document.querySelector('.introduce_conetnt');


let search = location.search.substring(1);
let { goods_id } = strToObj(search);

(async function () {
    let { data } = await axios.get(`/api/goods/details/${goods_id}`);
    title.innerHTML = data.goods_name;
    goodsImg.src = data.goods_big_logo;
    price.innerHTML = data.goods_price;
    introduce_conetnt.innerHTML = data.goods_introduce;
})();

// 点击加入购物车
let addCart = document.querySelector('#addCart');
addCart.onclick = async(e) => {
    e.returnValue = false;
    // 发送加入购物车的ajax请求
   let {data}= await axios({
            url:`/api/car/add/${goods_id}`,
            method:'put',
            headers:{
                'authorization':localStorage.getItem('token')
            }
    })
    if(data.code==400&&data.msg=='未登录'){
        alert('未登录，请登录之后再操作')
        // location.href('./login.html')
    }
    alert(data.msg)
}




